
############################
##2500 meters###############
############################
band <- individual_level[which(individual_level$dist<2500),]

model2500_1 <- lm(outcome1 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model2500_2 <- lm(outcome2 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model2500_3 <- lm(outcome3 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model2500_4 <- lm(outcome4 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)

obs2500 <- c(nobs(model2500_1),nobs(model2500_2),nobs(model2500_3),nobs(model2500_4))

model2500_1 <- coeftest(model2500_1, vcov=cluster.vcov(model2500_1, cluster = band$mkid14))
model2500_2 <- coeftest(model2500_2, vcov=cluster.vcov(model2500_2, cluster = band$mkid14))
model2500_3 <- coeftest(model2500_3, vcov=cluster.vcov(model2500_3, cluster = band$mkid14))
model2500_4 <- coeftest(model2500_4, vcov=cluster.vcov(model2500_4, cluster = band$mkid14))


############################
##5000 meters###############
############################
band <- individual_level[which(individual_level$dist<5000),]

model5000_1 <- lm(outcome1 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model5000_2 <- lm(outcome2 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model5000_3 <- lm(outcome3 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model5000_4 <- lm(outcome4 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)

obs5000 <- c(nobs(model5000_1),nobs(model5000_2),nobs(model5000_3),nobs(model5000_4))

model5000_1 <- coeftest(model5000_1, vcov=cluster.vcov(model5000_1, cluster = band$mkid14))
model5000_2 <- coeftest(model5000_2, vcov=cluster.vcov(model5000_2, cluster = band$mkid14))
model5000_3 <- coeftest(model5000_3, vcov=cluster.vcov(model5000_3, cluster = band$mkid14))
model5000_4 <- coeftest(model5000_4, vcov=cluster.vcov(model5000_4, cluster = band$mkid14))



############################
##7500 meters###############
############################
band <- individual_level[which(individual_level$dist<7500),]

model7500_1 <- lm(outcome1 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model7500_2 <- lm(outcome2 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model7500_3 <- lm(outcome3 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model7500_4 <- lm(outcome4 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)

obs7500 <- c(nobs(model7500_1),nobs(model7500_2),nobs(model7500_3),nobs(model7500_4))

model7500_1 <- coeftest(model7500_1, vcov=cluster.vcov(model7500_1, cluster = band$mkid14))
model7500_2 <- coeftest(model7500_2, vcov=cluster.vcov(model7500_2, cluster = band$mkid14))
model7500_3 <- coeftest(model7500_3, vcov=cluster.vcov(model7500_3, cluster = band$mkid14))
model7500_4 <- coeftest(model7500_4, vcov=cluster.vcov(model7500_4, cluster = band$mkid14))




############################
##10000 meters###############
############################
band <- individual_level[which(individual_level$dist<10000),]

model10000_1 <- lm(outcome1 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model10000_2 <- lm(outcome2 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model10000_3 <- lm(outcome3 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)
model10000_4 <- lm(outcome4 ~ opium_legal + forcing + opium_legal*forcing + border_lat + border_long, data=band)

obs10000 <- c(nobs(model10000_1),nobs(model10000_2),nobs(model10000_3),nobs(model10000_4))

model10000_1 <- coeftest(model10000_1, vcov=cluster.vcov(model10000_1, cluster = band$mkid14))
model10000_2 <- coeftest(model10000_2, vcov=cluster.vcov(model10000_2, cluster = band$mkid14))
model10000_3 <- coeftest(model10000_3, vcov=cluster.vcov(model10000_3, cluster = band$mkid14))
model10000_4 <- coeftest(model10000_4, vcov=cluster.vcov(model10000_4, cluster = band$mkid14))


models2500 = list(model2500_1, model2500_2, model2500_3, model2500_4)
models5000 = list(model5000_1, model5000_2, model5000_3, model5000_4)
models7500 = list(model7500_1, model7500_2, model7500_3, model7500_4)
models10000 = list(model10000_1, model10000_2, model10000_3, model10000_4)


table2500 =
  stargazer(models2500,
            type = "latex",
            title = "\\textsc{Effect of Opium Farm System on Ethnic Intolerance, Border Latitude and Longitude Controls}",
            label = "tab:table_main_fe",
            model.names = F,
            model.numbers = F,
            column.labels = c("Place of worship", "Live in village", "Live in neighborhood", "Rents room"),
            multicolumn = T,
            dep.var.labels = c("Would you protest if a member of another religion:"),
            star.cutoffs = c(0.1, 0.05, 0.01),
            add.lines = list(c("Observations", obs2500),
                             c('Bandwidth', rep(c('2500m'), 4))),
            covariate.labels = c("Opium Legal"),
            notes.align = "l"
  )

table5000 =
  stargazer(models5000,
            type = "latex",
            title = "Effect of Opium Farm System on Ethnic Intolerance",
            label = "tab:table_main",
            model.names = F,
            model.numbers = F,
            column.labels = c("Place of worship", "Live in village", "Live in neighborhood", "Rents room"),
            multicolumn = T,
            dep.var.labels = c("Would you protest if a member of another religion:"),
            star.cutoffs = c(0.1, 0.05, 0.01),
            add.lines = list(c("Observations", obs5000),
                             c('Bandwidth', rep(c('5000m'), 4))),
            covariate.labels = c("Opium Legal")
  )

table7500 =
  stargazer(models7500,
            type = "latex",
            title = "Effect of Opium Farm System on Ethnic Intolerance",
            label = "tab:table_main",
            model.names = F,
            model.numbers = F,
            column.labels = c("Place of worship", "Live in village", "Live in neighborhood", "Rents room"),
            multicolumn = T,
            dep.var.labels = c("Would you protest if a member of another religion:"),
            star.cutoffs = c(0.1, 0.05, 0.01),
            add.lines = list(c("Observations", obs7500),
                             c('Bandwidth', rep(c('7500m'), 4))),
            covariate.labels = c("Opium Legal")
  )

table10000 =
  stargazer(models10000,
            type = "latex",
            title = "Effect of Opium Farm System on Ethnic Intolerance",
            label = "tab:table_main",
            model.names = F,
            model.numbers = F,
            column.labels = c("Place of worship", "Live in village", "Live in neighborhood", "Rents room"),
            multicolumn = T,
            dep.var.labels = c("Would you protest if a member of another religion:"),
            star.cutoffs = c(0.1, 0.05, 0.01),
            add.lines = list(c("Observations", obs10000),
                             c('Bandwidth', rep(c('10000m'), 4))),
            covariate.labels = c("Opium Legal")
  )


note_text <- paste("Beta coefficients from OLS regression. Border latitude and longitude areas included as controls. Conventional standard errors clustered at the village level. 
                   The outcomes are drawn from a battery of questions that asked respondents if they would be upset or protest 
                   if a member of another religion (1) tried to build a place of worship nearby, (2) lived in their village, 
                   (3) lived in their neighborhood, or (4) rented a room in their house.")

table =
  table2500[-c(17:32, 38:40)] %>%
  append(., c("\\textbf{Panel A: 2500m} & & & & \\\\"), after = 14) %>%
  append(., table5000[c(15:16,33:37)]) %>%
  append(., c("\\textbf{Panel B: 5000m} & & & & \\\\"), after = 22) %>%
  append(., table7500[c(15:16, 33:37)]) %>%
  append(., c("\\textbf{Panel C: 7500m} & & & & \\\\"), after = 30) %>%
  append(., table10000[c(15:16, 33:40)]) %>%
  append(., c("\\textbf{Panel D: 10000m} & & & & \\\\"), after = 38)


write_latex_placebo(table[-47], note_text, './_4_outputs/tables/appendix_2_border_controls.tex', scale = 1)



